Country list <br />

<a href="#" id="country_add">Add</a>

<br />

<div id="country_list">

</div>


<script>
$(document).ready( function()
{
    function country_refresh()
    {
        $.post( 
            '/location/country-list',
            '',
            function( s, t )
            {
                if ( s['data']['countries'] )
                {
                    $('#country_list').html( s['data']['countries'] );
                    country_list_events();
                }
            },
            'json'
        );

    }

    function country_list_events()
    {
        $('.country_delete').click( function( e )
        {
            e.preventDefault();
            var id = $(this).attr( 'data-id' );
            $.post( 
                '/location/country-delete',
                'id=' + id,
                function( s, t )
                {
                    country_refresh();
                }
            );
        } );
        $('.country_modify').click( function( e )
        {
            e.preventDefault();

            var name = prompt( 'Rename the country to:' );
            var id = $(this).attr( 'data-id' );

            if ( name )
            {
                var p = 'id=' + id +
                        '&name=' + name;
                $.post( 
                    '/location/country-modify',
                    p,
                    function( s, t )
                    {
                        country_refresh();
                    }
                );
            }
        } );
    }

    $('#country_add').click( function( e )
    {
        e.preventDefault();

        var country = prompt( "Enter the name for the new country" );
        if ( country.length != 0 )
        {
            $.post( 
                '/location/country-add',
                'country=' + encodeURIComponent( country ),
                function( s, t )
                {
                    country_refresh();
                },
                'json'
            );
        }
    } );

    country_refresh();

} );
</script>